home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / viper / viper-ex.el < prev    next >
Encoding:
Text File  |  1995-08-31  |  60.9 KB  |  1,931 lines

  1. ;;; viper-ex.el -- functions implementing the Ex commands for Viper
  2. ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (require 'viper-util)
  22.  
  23. ;;; Variables
  24.  
  25. (defconst vip-ex-work-buf-name " *ex-working-space*")
  26. (defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
  27. (defconst vip-ex-tmp-buf-name " *ex-tmp*")
  28.  
  29.  
  30. ;;; Variable completion in :set command
  31.   
  32. ;; The list of Ex commands. Used for completing command names.
  33. (defconst ex-token-alist
  34.   '(("!") ("=") (">") ("&") ("~")
  35.     ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual") 
  36.     ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
  37.     ("substitute") ("submitReport") ("stop")  ("sr") ("source") ("shell")
  38.     ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
  39.     ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
  40.     ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
  41.     ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
  42.     ("chdir") ("cd") ("Buffer") ("buffer") ("args"))  )
  43.  
  44. ;; A-list of Ex variables that can be set using the :set command.
  45. (defconst ex-variable-alist 
  46.   '(("wrapscan") ("ws") ("wrapmargin") ("wm")
  47.     ("tab-stop-local") ("tsl") ("tabstop") ("ts")
  48.     ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
  49.     ("readonly") ("ro") 
  50.     ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
  51.     ("noreadonly") ("noro") ("nomagic") ("noma")
  52.     ("noignorecase") ("noic") ("noautoindent") ("noai")
  53.     ("magic") ("ma") ("ignorecase") ("ic") ("autoindent") ("ai")
  54.     ))
  55.  
  56.   
  57.  
  58. ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
  59. (defvar ex-token nil)
  60.  
  61. ;; Type of token. 
  62. ;; If non-nil, gives type of address; if nil, it is a command.
  63. (defvar ex-token-type nil)
  64.  
  65. ;; List of addresses passed to Ex command
  66. (defvar ex-addresses nil)
  67.  
  68. ;; It seems that this flag is used only for `#', `print', and `list', which
  69. ;; aren't implemented. Check later.
  70. (defvar ex-flag nil)
  71.  
  72. ;; "buffer" where Ex commands keep deleted data.
  73. ;; In Emacs terms, this is a register.
  74. (defvar ex-buffer nil)
  75.  
  76. ;; Value of ex count.
  77. (defvar ex-count nil)
  78.  
  79. ;; Flag for global command.
  80. (defvar ex-g-flag nil)
  81.  
  82. ;; If t, global command is executed on lines not matching ex-g-pat.
  83. (defvar ex-g-variant nil)
  84.  
  85. ;; Save reg-exp used in substitute.
  86. (defvar ex-reg-exp nil)
  87.  
  88.  
  89. ;; Replace pattern for substitute.
  90. (defvar ex-repl nil)
  91.  
  92. ;; Pattern for global command.
  93. (defvar ex-g-pat nil)
  94.  
  95. ;; `sh' doesn't seem to expand wildcards, like `*'
  96. (defconst ex-find-file-shell "csh"
  97.   "Shell in which to interpret wildcards. Must be csh, tcsh, or similar.
  98. Bourne shell doesn't seem to work here.")
  99. (defvar ex-find-file-shell-options "-f"
  100.   "*Options to pass to `ex-find-file-shell'.")
  101.  
  102. ;; Remembers the previous Ex tag.
  103. (defvar ex-tag nil)
  104.  
  105. ;; file used by Ex commands like :r, :w, :n
  106. (defvar ex-file nil)
  107.  
  108. ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
  109. (defvar ex-variant nil)
  110.  
  111. ;; Specified the offset of an Ex command, such as :read.
  112. (defvar ex-offset nil)
  113.  
  114. ;; Tells Ex that this is a w>> command.
  115. (defvar ex-append nil)
  116.  
  117. ;; File containing the shell command to be executed at Ex prompt,
  118. ;; e.g., :r !date
  119. (defvar ex-cmdfile nil)
  120.   
  121. ;; flag used in vip-ex-read-file-name to indicate that we may be reading
  122. ;; multiple file names. Used for :edit and :next
  123. (defvar vip-keep-reading-filename nil)
  124.  
  125. (defconst ex-cycle-other-window t
  126.   "*If t, :n and :b cycles through files and buffers in other window.
  127. Then :N and :B cycles in the current window. If nil, this behavior is
  128. reversed.")
  129.  
  130. (defconst ex-cycle-through-non-files nil
  131.   "*Cycle through *scratch* and other buffers that don't visit any file.")
  132.  
  133. ;; Last shell command executed with :! command.
  134. (defvar vip-ex-last-shell-com nil)
  135.   
  136. ;; Indicates if Minibuffer was exited temporarily in Ex-command.
  137. (defvar vip-incomplete-ex-cmd nil)
  138.   
  139. ;; Remembers the last ex-command prompt.
  140. (defvar vip-last-ex-prompt "")
  141.  
  142.  
  143. ;;; Code
  144.   
  145. ;; Check if ex-token is an initial segment of STR
  146. (defun vip-check-sub (str)
  147.   (let ((length (length ex-token)))
  148.     (if (and (<= length (length str))
  149.            (string= ex-token (substring str 0 length)))
  150.     (setq ex-token str)
  151.       (setq ex-token-type 'non-command))))
  152.  
  153. ;; Get a complete ex command
  154. (defun vip-get-ex-com-subr ()
  155.   (let (case-fold-search)
  156.     (set-mark (point))
  157.     (re-search-forward "[a-zA-Z][a-zA-Z]*")
  158.     (setq ex-token-type 'command)
  159.     (setq ex-token (buffer-substring (point) (mark t)))
  160.     (exchange-point-and-mark)
  161.     (cond ((looking-at "a")
  162.        (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
  163.          ((looking-at "ar") (vip-check-sub "args"))
  164.          (t (vip-check-sub "append"))))
  165.       ((looking-at "h") (vip-check-sub "help"))
  166.       ((looking-at "c")
  167.        (cond ((looking-at "cd") (vip-check-sub "cd"))
  168.          ((looking-at "ch") (vip-check-sub "chdir"))
  169.          ((looking-at "co") (vip-check-sub "copy"))
  170.          (t (vip-check-sub "change"))))
  171.       ((looking-at "d") (vip-check-sub "delete"))
  172.       ((looking-at "b") (vip-check-sub "buffer"))
  173.       ((looking-at "B") (vip-check-sub "Buffer"))
  174.       ((looking-at "e")
  175.        (if (looking-at "ex") (vip-check-sub "ex")
  176.          (vip-check-sub "edit")))
  177.       ((looking-at "f") (vip-check-sub "file"))
  178.       ((looking-at "g") (vip-check-sub "global"))
  179.       ((looking-at "i") (vip-check-sub "insert"))
  180.       ((looking-at "j") (vip-check-sub "join"))
  181.       ((looking-at "l") (vip-check-sub "list"))
  182.       ((looking-at "m")
  183.        (cond ((looking-at "map") (vip-check-sub "map"))
  184.          ((looking-at "mar") (vip-check-sub "mark"))
  185.          (t (vip-check-sub "move"))))
  186.       ((looking-at "k[a-z][^a-z]")
  187.        (setq ex-token "kmark")
  188.        (forward-char 1)
  189.        (exchange-point-and-mark))   ; this is canceled out by another
  190.                     ; exchange-point-and-mark at the end
  191.       ((looking-at "k") (vip-check-sub "kmark"))
  192.       ((looking-at "n") (if (looking-at "nu")
  193.                 (vip-check-sub "number")
  194.                   (vip-check-sub "next")))
  195.       ((looking-at "N") (vip-check-sub "Next"))
  196.       ((looking-at "o") (vip-check-sub "open"))
  197.       ((looking-at "p")
  198.        (cond ((looking-at "pre") (vip-check-sub "preserve"))
  199.          ((looking-at "pu") (vip-check-sub "put"))
  200.          ((looking-at "pw") (vip-check-sub "pwd"))
  201.          (t (vip-check-sub "print"))))
  202.       ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
  203.       ((looking-at "R") (vip-check-sub "RelatedFile"))
  204.       ((looking-at "q") (vip-check-sub "quit"))
  205.       ((looking-at "r")
  206.        (cond ((looking-at "rec") (vip-check-sub "recover"))
  207.          ((looking-at "rew") (vip-check-sub "rewind"))
  208.          (t (vip-check-sub "read"))))
  209.       ((looking-at "s")
  210.        (cond ((looking-at "se") (vip-check-sub "set"))
  211.          ((looking-at "sh") (vip-check-sub "shell"))
  212.          ((looking-at "so") (vip-check-sub "source"))
  213.          ((looking-at "sr") (vip-check-sub "sr"))
  214.          ((looking-at "st") (vip-check-sub "stop"))
  215.          ((looking-at "sus") (vip-check-sub "suspend"))
  216.          ((looking-at "subm") (vip-check-sub "submitReport"))
  217.          (t (vip-check-sub "substitute"))))
  218.       ((looking-at "t")
  219.        (if (looking-at "ta") (vip-check-sub "tag")
  220.          (vip-check-sub "transfer")))
  221.       ((looking-at "u")
  222.        (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
  223.          ((looking-at "unm") (vip-check-sub "unmap"))
  224.          (t (vip-check-sub "undo"))))
  225.       ((looking-at "v")
  226.        (cond ((looking-at "ve") (vip-check-sub "version"))
  227.          ((looking-at "vi") (vip-check-sub "visual"))
  228.          (t (vip-check-sub "vglobal"))))
  229.       ((looking-at "w")
  230.        (if (looking-at "wq") (vip-check-sub "wq")
  231.          (vip-check-sub "write")))
  232.       ((looking-at "W")
  233.        (if (looking-at "WW") 
  234.            (vip-check-sub "WWrite")
  235.          (vip-check-sub "Write")))
  236.       ((looking-at "x") (vip-check-sub "xit"))
  237.       ((looking-at "y") (vip-check-sub "yank"))
  238.       ((looking-at "z") (vip-check-sub "z")))
  239.     (exchange-point-and-mark)
  240.     ))
  241.  
  242. ;; Get an ex-token which is either an address or a command.
  243. ;; A token has a type, \(command, address, end-mark\), and a value
  244. (defun vip-get-ex-token ()
  245.   (save-window-excursion
  246.     (set-buffer vip-ex-work-buf)
  247.     (skip-chars-forward " \t|")
  248.     (cond ((looking-at "#")
  249.        (setq ex-token-type 'command)
  250.        (setq ex-token (char-to-string (following-char)))
  251.        (forward-char 1))
  252.       ((looking-at "[a-z]") (vip-get-ex-com-subr))
  253.       ((looking-at "\\.")
  254.        (forward-char 1)
  255.        (setq ex-token-type 'dot))
  256.       ((looking-at "[0-9]")
  257.        (set-mark (point))
  258.        (re-search-forward "[0-9]*")
  259.        (setq ex-token-type
  260.          (cond ((eq ex-token-type 'plus) 'add-number)
  261.                ((eq ex-token-type 'minus) 'sub-number)
  262.                (t 'abs-number)))
  263.        (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
  264.       ((looking-at "\\$")
  265.        (forward-char 1)
  266.        (setq ex-token-type 'end))
  267.       ((looking-at "%")
  268.        (forward-char 1)
  269.        (setq ex-token-type 'whole))
  270.       ((looking-at "+")
  271.        (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
  272.           (forward-char 1)
  273.           (insert "1")
  274.           (backward-char 1)
  275.           (setq ex-token-type 'plus))
  276.          ((looking-at "+[0-9]")
  277.           (forward-char 1)
  278.           (setq ex-token-type 'plus))
  279.          (t
  280.           (error vip-BadAddress))))
  281.       ((looking-at "-")
  282.        (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
  283.           (forward-char 1)
  284.           (insert "1")
  285.           (backward-char 1)
  286.           (setq ex-token-type 'minus))
  287.          ((looking-at "-[0-9]")
  288.           (forward-char 1)
  289.           (setq ex-token-type 'minus))
  290.          (t
  291.           (error vip-BadAddress))))
  292.       ((looking-at "/")
  293.        (forward-char 1)
  294.        (set-mark (point))
  295.        (let ((cont t))
  296.          (while (and (not (eolp)) cont)
  297.            ;;(re-search-forward "[^/]*/")
  298.            (re-search-forward "[^/]*\\(/\\|\n\\)")
  299.            (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
  300.            (setq cont nil))))
  301.        (backward-char 1)
  302.        (setq ex-token (buffer-substring (point) (mark t)))
  303.        (if (looking-at "/") (forward-char 1))
  304.        (setq ex-token-type 'search-forward))
  305.       ((looking-at "\\?")
  306.        (forward-char 1)
  307.        (set-mark (point))
  308.        (let ((cont t))
  309.          (while (and (not (eolp)) cont)
  310.            ;;(re-search-forward "[^\\?]*\\?")
  311.            (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
  312.            (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
  313.            (setq cont nil))
  314.            (backward-char 1)
  315.            (if (not (looking-at "\n")) (forward-char 1))))
  316.        (setq ex-token-type 'search-backward)
  317.        (setq ex-token (buffer-substring (1- (point)) (mark t))))
  318.       ((looking-at ",")
  319.        (forward-char 1)
  320.        (setq ex-token-type 'comma))
  321.       ((looking-at ";")
  322.        (forward-char 1)
  323.        (setq ex-token-type 'semi-colon))
  324.       ((looking-at "[!=><&~]")
  325.        (setq ex-token-type 'command)
  326.        (setq ex-token (char-to-string (following-char)))
  327.        (forward-char 1))
  328.       ((looking-at "'")
  329.        (setq ex-token-type 'goto-mark)
  330.        (forward-char 1)
  331.        (cond ((looking-at "'") (setq ex-token nil))
  332.          ((looking-at "[a-z]") (setq ex-token (following-char)))
  333.          (t (error "Marks are ' and a-z")))
  334.        (forward-char 1))
  335.       ((looking-at "\n")
  336.        (setq ex-token-type 'end-mark)
  337.        (setq ex-token "goto"))
  338.       (t
  339.        (error vip-BadExCommand)))))
  340.  
  341. ;; Reads Ex command. Tries to determine if it has to exit because command
  342. ;; is complete or invalid. If not, keeps reading command.
  343. (defun ex-cmd-read-exit ()
  344.   (interactive)
  345.   (setq vip-incomplete-ex-cmd t)
  346.   (let ((quit-regex1 (concat
  347.               "\\("
  348.               "set[ \t]*" "\\|" "edit[ \t]*" "\\|" "[nN]ext[ \t]*"
  349.               "\\|" "unm[ \t]*" "\\|" "^[ \t]*rep"
  350.               "\\)"))
  351.     (quit-regex2 (concat
  352.               "[a-zA-Z][ \t]*"
  353.               "\\(" "!" "\\|" ">>"
  354.               "\\|" "\\+[0-9]+"
  355.               "\\)"
  356.               "*[ \t]*$"))
  357.     (stay-regex (concat
  358.              "\\("
  359.              "^[ \t]*$" "\\|" "[ktgjmsz][ \t]*$" "\\|" "^[ \t]*ab.*"
  360.              "\\|" "tr[ansfer \t]*" "\\|" "sr[ \t]*"
  361.              "\\|" "mo.*" "\\|" "^[ \t]*k?ma[^p]*"
  362.              "\\|" "^[ \t]*fi.*" "\\|" "v?gl.*" "\\|" "[vg][ \t]*$"
  363.              "\\|" "jo.*" "\\|" "^[ \t]*ta.*" "\\|" "^[ \t]*una.*"
  364.              "\\|" "^[ \t]*su.*" "\\|['`][a-z][ \t]*"
  365.              "\\|" "![ \t]*[a-zA-Z].*"
  366.              "\\)"
  367.              "!*")))
  368.     
  369.     (save-window-excursion ;; put cursor at the end of the Ex working buffer
  370.       (set-buffer vip-ex-work-buf)
  371.       (goto-char (point-max)))
  372.     (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
  373.       ((vip-looking-back stay-regex)  (insert " "))
  374.       ((vip-looking-back quit-regex2) (exit-minibuffer))
  375.       (t (insert " ")))))
  376.   
  377. ;; complete Ex command
  378. (defun ex-cmd-complete ()
  379.   (interactive)
  380.   (let (save-pos dist compl-list string-to-complete completion-result)
  381.     
  382.     (save-excursion
  383.       (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
  384.         save-pos (point)))
  385.     
  386.     (if (or (= dist 0)
  387.         (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
  388.         (vip-looking-back
  389.          "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
  390.     ;; Preceding characters are not the ones allowed in an Ex command
  391.     ;; or we have typed past command name.
  392.     ;; Note: we didn't do parsing, so there may be surprises.
  393.     (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
  394.         (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
  395.         (looking-at "[^ \t\n\C-m]"))
  396.         nil
  397.       (with-output-to-temp-buffer "*Completions*" 
  398.         (display-completion-list
  399.          (vip-alist-to-list ex-token-alist))))
  400.       ;; Preceding chars may be part of a command name
  401.       (setq string-to-complete (buffer-substring save-pos (point)))
  402.       (setq completion-result
  403.         (try-completion string-to-complete ex-token-alist))
  404.       
  405.       (cond ((eq completion-result t)  ; exact match--do nothing
  406.          (vip-tmp-insert-at-eob " (Sole completion)"))
  407.         ((eq completion-result nil)
  408.          (vip-tmp-insert-at-eob " (No match)"))
  409.         (t  ;; partial completion
  410.          (goto-char save-pos)
  411.          (delete-region (point) (point-max))
  412.          (insert completion-result)
  413.          (let (case-fold-search)
  414.            (setq compl-list
  415.              (vip-filter-alist (concat "^" completion-result)
  416.                        ex-token-alist)))
  417.          (if (> (length compl-list) 1)
  418.          (with-output-to-temp-buffer "*Completions*" 
  419.            (display-completion-list
  420.             (vip-alist-to-list (reverse compl-list)))))))
  421.       )))
  422.     
  423.  
  424. ;; Read Ex commands 
  425. ;; Ex commands themselves are implemented in viper-ex.el
  426. (defun vip-ex (&optional string)
  427.   (interactive)
  428.   (or string
  429.       (setq ex-g-flag nil
  430.         ex-g-variant nil))
  431.   (let* ((map (copy-keymap minibuffer-local-map))
  432.      (address nil)
  433.      (cont t)
  434.      (dot (point))
  435.      prev-token-type com-str)
  436.      
  437.     (vip-add-keymap vip-ex-cmd-map map)
  438.     
  439.     (setq com-str (or string (vip-read-string-with-history
  440.                   ":" 
  441.                   nil
  442.                   'vip-ex-history
  443.                   (car vip-ex-history)
  444.                   map)))
  445.     (save-window-excursion
  446.       ;; just a precaution
  447.       (or (vip-buffer-live-p vip-ex-work-buf)
  448.       (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)))
  449.       (set-buffer vip-ex-work-buf)
  450.       (delete-region (point-min) (point-max))
  451.       (insert com-str "\n")
  452.       (goto-char (point-min)))
  453.     (setq ex-token-type nil
  454.       ex-addresses nil)
  455.     (while cont
  456.       (vip-get-ex-token)
  457.       (cond ((memq ex-token-type '(command end-mark))
  458.          (if address (setq ex-addresses (cons address ex-addresses)))
  459.          (cond ((string= ex-token "global")
  460.             (ex-global nil)
  461.             (setq cont nil))
  462.            ((string= ex-token "vglobal")
  463.             (ex-global t)
  464.             (setq cont nil))
  465.            (t
  466.             (vip-execute-ex-command)
  467.             (save-window-excursion
  468.               (set-buffer vip-ex-work-buf)
  469.               (skip-chars-forward " \t")
  470.               (cond ((looking-at "|")
  471.                  (forward-char 1))
  472.                 ((looking-at "\n")
  473.                  (setq cont nil))
  474.                 (t (error "`%s': %s" ex-token vip-SpuriousText)))
  475.               ))
  476.            ))
  477.         ((eq ex-token-type 'non-command)
  478.          (error (format "`%s': %s" ex-token vip-BadExCommand)))
  479.         ((eq ex-token-type 'whole)
  480.          (setq address nil)
  481.          (setq ex-addresses
  482.            (if ex-addresses
  483.                (cons (point-max) ex-addresses)
  484.              (cons (point-max) (cons (point-min) ex-addresses)))))
  485.         ((eq ex-token-type 'comma)
  486.          (if (eq prev-token-type 'whole)
  487.          (setq address (point-min)))
  488.          (setq ex-addresses
  489.            (cons (if (null address) (point) address) ex-addresses)))
  490.         ((eq ex-token-type 'semi-colon)
  491.          (if (eq prev-token-type 'whole)
  492.          (setq address (point-min)))
  493.          (if address (setq dot address))
  494.          (setq ex-addresses
  495.            (cons (if (null address) (point) address) ex-addresses)))
  496.         (t (let ((ans (vip-get-ex-address-subr address dot)))
  497.          (if ans (setq address ans)))))
  498.       (setq prev-token-type ex-token-type))))
  499.       
  500.  
  501. ;; Get a regular expression and set `ex-variant', if found
  502. (defun vip-get-ex-pat ()
  503.   (save-window-excursion
  504.     (set-buffer vip-ex-work-buf)
  505.     (skip-chars-forward " \t")
  506.     (if (looking-at "!")
  507.     (progn
  508.       (setq ex-g-variant (not ex-g-variant)
  509.         ex-g-flag (not ex-g-flag))
  510.       (forward-char 1)
  511.       (skip-chars-forward " \t")))
  512.     (let ((c (following-char)))
  513.       (if (string-match "[0-9A-Za-z]" (format "%c" c))
  514.       (error
  515.        "Global regexp must be inside matching non-alphanumeric chars"))
  516.       (if (looking-at "[^\\\\\n]")
  517.       (progn
  518.         (forward-char 1)
  519.         (set-mark (point))
  520.         (let ((cont t))
  521.           (while (and (not (eolp)) cont)
  522.         (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
  523.             (if (member ex-token '("global" "vglobal"))
  524.             (error
  525.              "Missing closing delimiter for global regexp")
  526.               (goto-char (point-max))))
  527.         (if (not (vip-looking-back
  528.               (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
  529.             (setq cont nil))))
  530.         (setq ex-token
  531.           (if (= (mark t) (point)) ""
  532.             (buffer-substring (1- (point)) (mark t))))
  533.         (backward-char 1))
  534.     (setq ex-token nil))
  535.       c)))
  536.  
  537. ;; get an ex command
  538. (defun vip-get-ex-command ()
  539.   (save-window-excursion
  540.     (set-buffer vip-ex-work-buf)
  541.     (if (looking-at "/") (forward-char 1))
  542.     (skip-chars-forward " \t")
  543.     (cond ((looking-at "[a-z]")
  544.        (vip-get-ex-com-subr)
  545.        (if (eq ex-token-type 'non-command)
  546.            (error "`%s': %s" ex-token vip-BadExCommand)))
  547.       ((looking-at "[!=><&~]")
  548.        (setq ex-token (char-to-string (following-char)))
  549.        (forward-char 1))
  550.       (t (error vip-BadExCommand)))))
  551.  
  552. ;; Get an Ex option g or c
  553. (defun vip-get-ex-opt-gc (c)
  554.   (save-window-excursion
  555.     (set-buffer vip-ex-work-buf)
  556.     (if (looking-at (format "%c" c)) (forward-char 1))
  557.     (skip-chars-forward " \t")
  558.     (cond ((looking-at "g")
  559.        (setq ex-token "g")
  560.        (forward-char 1)
  561.        t)
  562.       ((looking-at "c")
  563.        (setq ex-token "c")
  564.        (forward-char 1)
  565.        t)
  566.       (t nil))))
  567.  
  568. ;; Compute default addresses.  WHOLE-FLAG means use the whole buffer
  569. (defun vip-default-ex-addresses (&optional whole-flag)
  570.   (cond ((null ex-addresses)
  571.      (setq ex-addresses
  572.            (if whole-flag
  573.            (cons (point-max) (cons (point-min) nil))
  574.          (cons (point) (cons (point) nil)))))
  575.     ((null (cdr ex-addresses))
  576.      (setq ex-addresses
  577.            (cons (car ex-addresses) ex-addresses)))))
  578.  
  579. ;; Get an ex-address as a marker and set ex-flag if a flag is found
  580. (defun vip-get-ex-address ()
  581.   (let ((address (point-marker)) (cont t))
  582.     (setq ex-token "")
  583.     (setq ex-flag nil)
  584.     (while cont
  585.       (vip-get-ex-token)
  586.       (cond ((eq ex-token-type 'command)
  587.          (if (member ex-token '("print" "list" "#"))
  588.          (progn
  589.            (setq ex-flag t
  590.              cont nil))
  591.            (error "Address expected in this Ex command")))
  592.         ((eq ex-token-type 'end-mark)
  593.          (setq cont nil))
  594.         ((eq ex-token-type 'whole)
  595.          (error "Trailing address expected"))
  596.         ((eq ex-token-type 'comma)
  597.          (error "`%s': %s" ex-token vip-SpuriousText))
  598.         (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
  599.          (if ans (setq address ans))))))
  600.     address))
  601.  
  602. ;; Returns an address as a point
  603. (defun vip-get-ex-address-subr (old-address dot)
  604.   (let ((address nil))
  605.     (if (null old-address) (setq old-address dot))
  606.     (cond ((eq ex-token-type 'dot)
  607.        (setq address dot))
  608.       ((eq ex-token-type 'add-number)
  609.        (save-excursion
  610.          (goto-char old-address)
  611.          (forward-line (if (= old-address 0) (1- ex-token) ex-token))
  612.          (setq address (point-marker))))
  613.       ((eq ex-token-type 'sub-number)
  614.        (save-excursion
  615.          (goto-char old-address)
  616.          (forward-line (- ex-token))
  617.          (setq address (point-marker))))
  618.       ((eq ex-token-type 'abs-number)
  619.        (save-excursion
  620.          (goto-char (point-min))
  621.          (if (= ex-token 0) (setq address 0)
  622.            (forward-line (1- ex-token))
  623.            (setq address (point-marker)))))
  624.       ((eq ex-token-type 'end)
  625.        (setq address (point-max-marker)))
  626.       ((eq ex-token-type 'plus) t)  ; do nothing
  627.       ((eq ex-token-type 'minus) t) ; do nothing
  628.       ((eq ex-token-type 'search-forward)
  629.        (save-excursion
  630.          (ex-search-address t)
  631.          (setq address (point-marker))))
  632.       ((eq ex-token-type 'search-backward)
  633.        (save-excursion
  634.          (ex-search-address nil)
  635.          (setq address (point-marker))))
  636.       ((eq ex-token-type 'goto-mark)
  637.        (save-excursion
  638.          (if (null ex-token)
  639.          (exchange-point-and-mark)
  640.            (goto-char (vip-register-to-point
  641.                (1+ (- ex-token ?a)) 'enforce-buffer)))
  642.          (setq address (point-marker)))))
  643.     address))
  644.  
  645.  
  646. ;; Search pattern and set address
  647. (defun ex-search-address (forward)
  648.   (if (string= ex-token "")
  649.       (if (null vip-s-string)
  650.       (error vip-NoPrevSearch)
  651.     (setq ex-token vip-s-string))
  652.     (setq vip-s-string ex-token))
  653.   (if forward
  654.       (progn
  655.     (forward-line 1)
  656.     (re-search-forward ex-token))
  657.     (forward-line -1)
  658.     (re-search-backward ex-token)))
  659.  
  660. ;; Get a buffer name and set `ex-count' and `ex-flag' if found
  661. (defun vip-get-ex-buffer ()
  662.   (setq ex-buffer nil)
  663.   (setq ex-count nil)
  664.   (setq ex-flag nil)
  665.   (save-window-excursion
  666.     (set-buffer vip-ex-work-buf)
  667.     (skip-chars-forward " \t")
  668.     (if (looking-at "[a-zA-Z]")
  669.     (progn
  670.       (setq ex-buffer (following-char))
  671.       (forward-char 1)
  672.       (skip-chars-forward " \t")))
  673.     (if (looking-at "[0-9]")
  674.     (progn
  675.       (set-mark (point))
  676.       (re-search-forward "[0-9][0-9]*")
  677.       (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
  678.       (skip-chars-forward " \t")))
  679.     (if (looking-at "[pl#]")
  680.     (progn
  681.       (setq ex-flag t)
  682.       (forward-char 1)))
  683.     (if (not (looking-at "[\n|]"))
  684.     (error "`%s': %s" ex-token vip-SpuriousText))))
  685.  
  686. (defun vip-get-ex-count ()
  687.   (setq ex-variant nil
  688.     ex-count nil
  689.     ex-flag nil)
  690.   (save-window-excursion
  691.     (set-buffer vip-ex-work-buf)
  692.     (skip-chars-forward " \t")
  693.     (if (looking-at "!")
  694.     (progn
  695.       (setq ex-variant t)
  696.       (forward-char 1)))
  697.     (skip-chars-forward " \t")
  698.     (if (looking-at "[0-9]")
  699.     (progn
  700.       (set-mark (point))
  701.       (re-search-forward "[0-9][0-9]*")
  702.       (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
  703.       (skip-chars-forward " \t")))
  704.     (if (looking-at "[pl#]")
  705.     (progn
  706.       (setq ex-flag t)
  707.       (forward-char 1)))
  708.     (if (not (looking-at "[\n|]"))
  709.     (error "`%s': %s"
  710.            (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
  711.  
  712. ;; Expand \% and \# in ex command
  713. (defun ex-expand-filsyms (cmd buf)
  714.   (let (cf pf ret)
  715.     (save-excursion 
  716.       (set-buffer buf)
  717.       (setq cf buffer-file-name)
  718.       (setq pf (ex-next nil t))) ; this finds alternative file name
  719.     (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
  720.     (error "No current file to substitute for `\%'"))
  721.     (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
  722.     (error "No alternate file to substitute for `#'"))
  723.     (save-excursion
  724.       (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
  725.       (erase-buffer)
  726.       (insert cmd)
  727.       (goto-char (point-min))
  728.       (while (re-search-forward "%\\|#" nil t)
  729.     (let ((data (match-data)) 
  730.           (char (buffer-substring (match-beginning 0) (match-end 0))))
  731.       (if (vip-looking-back (concat "\\\\" char))
  732.           (replace-match char)
  733.         (store-match-data data)
  734.         (if (string= char "%")
  735.         (replace-match cf)
  736.           (replace-match pf)))))
  737.       (end-of-line)
  738.       (setq ret (buffer-substring (point-min) (point)))
  739.       (message "%s" ret))
  740.     ret))
  741.  
  742. ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
  743. (defun vip-get-ex-file ()
  744.   (let (prompt)
  745.     (setq ex-file nil
  746.       ex-variant nil
  747.       ex-append nil
  748.       ex-offset nil
  749.       ex-cmdfile nil)
  750.     (save-excursion
  751.       (save-window-excursion
  752.     (set-buffer vip-ex-work-buf)
  753.     (skip-chars-forward " \t")
  754.     (if (looking-at "!")
  755.         (if (not (vip-looking-back "[ \t]"))
  756.         (progn
  757.           (setq ex-variant t)
  758.           (forward-char 1)
  759.           (skip-chars-forward " \t"))
  760.           (setq ex-cmdfile t)
  761.           (forward-char 1)
  762.           (skip-chars-forward " \t")))
  763.     (if (looking-at ">>")
  764.         (progn
  765.           (setq ex-append t
  766.             ex-variant t)
  767.           (forward-char 2)
  768.           (skip-chars-forward " \t")))
  769.     (if (looking-at "+")
  770.         (progn
  771.           (forward-char 1)
  772.           (set-mark (point))
  773.           (re-search-forward "[ \t\n]")
  774.           (backward-char 1)
  775.           (setq ex-offset (buffer-substring (point) (mark t)))
  776.           (forward-char 1)
  777.           (skip-chars-forward " \t")))
  778.     ;; this takes care of :r, :w, etc., when they get file names
  779.     ;; from the history list
  780.     (if (member ex-token '("read" "write" "edit" "visual" "next"))
  781.         (progn
  782.           (setq ex-file (buffer-substring (point)  (1- (point-max))))
  783.           (setq ex-file
  784.             ;; For :e, match multiple non-white strings separated
  785.             ;; by white. For others, find the first non-white string
  786.             (if (string-match
  787.              (if (string= ex-token "edit")
  788.                  "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
  789.                "[^ \t\n]+")
  790.              ex-file)
  791.             (progn
  792.               ;; if file name comes from history, don't leave
  793.               ;; minibuffer when the user types space
  794.               (setq vip-incomplete-ex-cmd nil)
  795.               ;; this must be the last clause in this progn
  796.               (substring ex-file (match-beginning 0) (match-end 0))
  797.               )
  798.               ""))
  799.           ;; this leaves only the command name in the work area
  800.           ;; file names are gone
  801.           (delete-region (point) (1- (point-max)))
  802.           ))
  803.     (goto-char (point-max))
  804.     (skip-chars-backward " \t\n")
  805.     (setq prompt (buffer-substring (point-min) (point)))
  806.     ))
  807.     
  808.     (setq vip-last-ex-prompt prompt)
  809.     
  810.     ;; If we just finished reading command, redisplay prompt
  811.     (if vip-incomplete-ex-cmd
  812.     (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
  813.       ;; file was typed in-line
  814.       (setq ex-file (or ex-file "")))
  815.     ))
  816.  
  817.  
  818. ;; Completes file name or exits minibuffer. If Ex command accepts multiple
  819. ;; file names, arranges to re-enter the minibuffer.
  820. (defun vip-complete-filename-or-exit ()
  821.   (interactive)
  822.   (setq vip-keep-reading-filename t) 
  823.   ;; don't exit if directory---ex-commands don't 
  824.   (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
  825.     (t (minibuffer-complete-word))))
  826.      
  827.            
  828. (defun ex-cmd-accepts-multiple-files-p (token)
  829.   (member token '("edit" "next" "Next")))
  830.  
  831. ;; If user doesn't enter anything, then "" is returned, i.e., the
  832. ;; prompt-directory is not returned.
  833. (defun vip-ex-read-file-name (prompt)
  834.   (let* ((str "")
  835.      (minibuffer-local-completion-map
  836.       (copy-keymap minibuffer-local-completion-map))
  837.      beg end cont val)
  838.     
  839.     (vip-add-keymap ex-read-filename-map minibuffer-local-completion-map) 
  840.             
  841.     (setq cont (setq vip-keep-reading-filename t))
  842.     (while cont
  843.       (setq vip-keep-reading-filename nil
  844.         val (read-file-name (concat prompt str) nil default-directory)
  845.         str  (concat str (if (equal val "") "" " ")
  846.              val (if (equal val "") "" " ")))
  847.              
  848.       ;; Only edit, next, and Next commands accept multiple files.
  849.       ;; vip-keep-reading-filename is set in the anonymous function that is
  850.       ;; bound to " " in ex-read-filename-map.
  851.       (setq cont (and vip-keep-reading-filename
  852.               (ex-cmd-accepts-multiple-files-p ex-token)))
  853.       )
  854.     
  855.     (setq beg (string-match "[^ \t]" str)   ; delete leading blanks
  856.       end (string-match "[ \t]*$" str)) ; delete trailing blanks
  857.     (if (member ex-token '("read" "write"))
  858.       (if (string-match "[\t ]*!" str)
  859.           ;; this is actually a shell command
  860.           (progn
  861.         (setq ex-cmdfile t)
  862.         (setq beg (1+ beg))
  863.         (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
  864.     (substring str (or beg 0) end)))
  865.  
  866. ;; Execute ex command using the value of addresses
  867. (defun vip-execute-ex-command ()
  868.   (vip-deactivate-mark)
  869.   (cond ((string= ex-token "args") (ex-args))
  870.     ((string= ex-token "copy") (ex-copy nil))
  871.     ((string= ex-token "cd") (ex-cd))
  872.     ((string= ex-token "chdir") (ex-cd))
  873.     ((string= ex-token "delete") (ex-delete))
  874.     ((string= ex-token "edit") (ex-edit))
  875.     ((string= ex-token "file") (vip-info-on-file))
  876.     ((string= ex-token "goto") (ex-goto))
  877.     ((string= ex-token "help") (ex-help))
  878.     ((string= ex-token "join") (ex-line "join"))
  879.     ((string= ex-token "kmark") (ex-mark))
  880.     ((string= ex-token "mark") (ex-mark))
  881.     ((string= ex-token "map") (ex-map))
  882.     ((string= ex-token "move") (ex-copy t))
  883.     ((string= ex-token "next") (ex-next ex-cycle-other-window))
  884.     ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
  885.     ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
  886.     ((string= ex-token "put") (ex-put))
  887.     ((string= ex-token "pwd") (ex-pwd))
  888.     ((string= ex-token "preserve") (ex-preserve))
  889.     ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
  890.     ((string= ex-token "quit") (ex-quit))
  891.     ((string= ex-token "read") (ex-read))
  892.     ((string= ex-token "recover") (ex-recover))
  893.     ((string= ex-token "rewind") (ex-rewind))
  894.     ((string= ex-token "submitReport") (vip-submit-report))
  895.     ((string= ex-token "set") (ex-set))
  896.     ((string= ex-token "shell") (ex-shell))
  897.     ((string= ex-token "source") (ex-source))
  898.     ((string= ex-token "sr") (ex-substitute t t))
  899.     ((string= ex-token "substitute") (ex-substitute))
  900.     ((string= ex-token "suspend") (suspend-emacs))
  901.     ((string= ex-token "stop") (suspend-emacs))
  902.     ((string= ex-token "transfer") (ex-copy nil))
  903.     ((string= ex-token "buffer") (if ex-cycle-other-window
  904.                      (vip-switch-to-buffer-other-window)
  905.                      (vip-switch-to-buffer)))
  906.     ((string= ex-token "Buffer") (if ex-cycle-other-window
  907.                      (vip-switch-to-buffer)
  908.                      (vip-switch-to-buffer-other-window)))
  909.     ((string= ex-token "tag") (ex-tag))
  910.     ((string= ex-token "undo") (vip-undo))
  911.     ((string= ex-token "unmap") (ex-unmap))
  912.     ((string= ex-token "version") (vip-version))
  913.     ((string= ex-token "visual") (ex-edit))
  914.     ((string= ex-token "write") (ex-write nil))
  915.     ((string= ex-token "Write") (save-some-buffers))
  916.     ((string= ex-token "wq") (ex-write t))
  917.     ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
  918.     ((string= ex-token "xit") (ex-write t))
  919.     ((string= ex-token "yank") (ex-yank))
  920.     ((string= ex-token "!") (ex-command))
  921.     ((string= ex-token "=") (ex-line-no))
  922.     ((string= ex-token ">") (ex-line "right"))
  923.     ((string= ex-token "<") (ex-line "left"))
  924.     ((string= ex-token "&") (ex-substitute t))
  925.     ((string= ex-token "~") (ex-substitute t t))
  926.     ((or (string= ex-token "append")
  927.          (string= ex-token "change")
  928.          (string= ex-token "insert")
  929.          (string= ex-token "open"))
  930.      (error
  931.       (format "`%s': Obsolete command, not supported by Viper"
  932.           ex-token)))
  933.     ((or (string= ex-token "abbreviate")
  934.          (string= ex-token "unabbreviate"))
  935.      (error
  936.       (format
  937.        "`%s': Vi-style abbrevs are obsolete. Use the more powerful Emacs abbrevs"
  938.        ex-token)))
  939.     ((or (string= ex-token "list")
  940.          (string= ex-token "print")
  941.          (string= ex-token "z")
  942.          (string= ex-token "#"))
  943.      (error
  944.       (format "`%s': Command not implemented in Viper" ex-token)))
  945.     (t (error (format "`%s': %s" ex-token vip-BadExCommand)))))
  946.  
  947. (defun vip-undisplayed-files ()
  948.   (mapcar
  949.    (function 
  950.     (lambda (b) 
  951.       (if (null (get-buffer-window b))
  952.       (let ((f (buffer-file-name b)))
  953.         (if f f
  954.           (if ex-cycle-through-non-files 
  955.           (let ((s (buffer-name b)))
  956.             (if (string= " " (substring s 0 1))
  957.             nil
  958.               s))
  959.         nil)))
  960.     nil)))
  961.    (buffer-list)))
  962.  
  963.  
  964. (defun ex-args ()
  965.   (let ((l (vip-undisplayed-files))
  966.     (args "")
  967.     (file-count 1))
  968.     (while (not (null l))
  969.       (if (car l) 
  970.       (setq args (format "%s %d) %s\n" args file-count (car l))
  971.         file-count (1+ file-count)))
  972.       (setq l (cdr l)))
  973.     (if (string= args "")
  974.     (message "All files are already displayed")
  975.       (save-excursion
  976.     (save-window-excursion
  977.       (with-output-to-temp-buffer " *vip-info*"
  978.         (princ "\n\nThese files are not displayed in any window.\n")
  979.         (princ "\n=============\n")
  980.         (princ args)
  981.         (princ "\n=============\n")
  982.         (princ "\nThe numbers can be given as counts to :next. ")
  983.         (princ "\n\nPress any key to continue...\n\n"))
  984.       (vip-read-event))))))
  985.  
  986. ;; Ex cd command. Default directory of this buffer changes
  987. (defun ex-cd ()
  988.   (vip-get-ex-file)
  989.   (if (string= ex-file "")
  990.       (setq ex-file "~"))
  991.   (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
  992.  
  993. ;; Ex copy and move command.  DEL-FLAG means delete
  994. (defun ex-copy (del-flag)
  995.   (vip-default-ex-addresses)
  996.   (let ((address (vip-get-ex-address))
  997.     (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  998.     (goto-char end)
  999.     (save-excursion
  1000.       (push-mark beg t)
  1001.       (vip-enlarge-region (mark t) (point))
  1002.       (if del-flag
  1003.       (kill-region (point) (mark t))
  1004.     (copy-region-as-kill (point) (mark t)))
  1005.       (if ex-flag
  1006.       (progn
  1007.         (with-output-to-temp-buffer "*copy text*"
  1008.           (princ
  1009.            (if (or del-flag ex-g-flag ex-g-variant)
  1010.            (current-kill 0)
  1011.          (buffer-substring (point) (mark t)))))
  1012.         (condition-case nil
  1013.         (progn
  1014.           (read-string "[Hit return to continue] ")
  1015.           (save-excursion (kill-buffer "*copy text*")))
  1016.           (quit (save-excursion (kill-buffer "*copy text*"))
  1017.             (signal 'quit nil))))))
  1018.     (if (= address 0)
  1019.     (goto-char (point-min))
  1020.       (goto-char address)
  1021.       (forward-line 1))
  1022.       (insert (current-kill 0))))
  1023.  
  1024. ;; Ex delete command
  1025. (defun ex-delete ()
  1026.   (vip-default-ex-addresses)
  1027.   (vip-get-ex-buffer)
  1028.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1029.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1030.     (save-excursion
  1031.       (vip-enlarge-region beg end)
  1032.       (exchange-point-and-mark)
  1033.       (if ex-count
  1034.       (progn
  1035.         (set-mark (point))
  1036.         (forward-line (1- ex-count)))
  1037.     (set-mark end))
  1038.       (vip-enlarge-region (point) (mark t))
  1039.       (if ex-flag
  1040.       ;; show text to be deleted and ask for confirmation
  1041.       (progn
  1042.         (with-output-to-temp-buffer " *delete text*"
  1043.           (princ (buffer-substring (point) (mark t))))
  1044.         (condition-case nil
  1045.         (read-string "[Hit return to continue] ")
  1046.           (quit
  1047.            (save-excursion (kill-buffer " *delete text*"))
  1048.            (error "")))
  1049.         (save-excursion (kill-buffer " *delete text*")))
  1050.     (if ex-buffer
  1051.         (cond ((vip-valid-register ex-buffer '(Letter))
  1052.            (vip-append-to-register
  1053.             (downcase ex-buffer) (point) (mark t)))
  1054.           ((vip-valid-register ex-buffer)
  1055.            (copy-to-register ex-buffer (point) (mark t) nil))
  1056.           (t (error vip-InvalidRegister ex-buffer))))
  1057.     (kill-region (point) (mark t))))))
  1058.  
  1059.  
  1060.  
  1061. ;; Ex edit command
  1062. ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
  1063. ;; asked if current buffer should really be discarded.
  1064. ;; This command can take multiple file names. It replaces the current buffer
  1065. ;; with the first file in its argument list
  1066. (defun ex-edit (&optional file)
  1067.   (if (not file)
  1068.       (vip-get-ex-file))
  1069.   (cond ((and (string= ex-file "") buffer-file-name)
  1070.      (setq ex-file  (abbreviate-file-name (buffer-file-name))))
  1071.     ((string= ex-file "")
  1072.      (error vip-NoFileSpecified)))
  1073.       
  1074.   (let (msg do-edit)
  1075.     (if buffer-file-name
  1076.     (cond ((buffer-modified-p)
  1077.            (setq msg
  1078.              (format "Buffer %s is modified. Edit buffer? "
  1079.                  (buffer-name))
  1080.              do-edit t))
  1081.           ((not (verify-visited-file-modtime (current-buffer)))
  1082.            (setq msg
  1083.              (format "File %s changed on disk.  Reread from disk? "
  1084.                  buffer-file-name)
  1085.              do-edit t))
  1086.           (t (setq do-edit nil))))
  1087.       
  1088.     (if do-edit
  1089.     (if (yes-or-no-p msg)
  1090.         (progn
  1091.           (set-buffer-modified-p nil)
  1092.           (kill-buffer (current-buffer)))
  1093.       (message "Buffer %s was left intact" (buffer-name))))
  1094.     ) ; let
  1095.   
  1096.   (if (null (setq file (get-file-buffer ex-file)))
  1097.       (progn 
  1098.     (ex-find-file ex-file)
  1099.     (vip-change-state-to-vi)
  1100.     (goto-char (point-min)))
  1101.     (switch-to-buffer file))
  1102.   (if ex-offset
  1103.       (progn
  1104.     (save-window-excursion
  1105.       (set-buffer vip-ex-work-buf)
  1106.       (delete-region (point-min) (point-max))
  1107.       (insert ex-offset "\n")
  1108.       (goto-char (point-min)))
  1109.     (goto-char (vip-get-ex-address))
  1110.     (beginning-of-line)))
  1111.   (ex-fixup-history vip-last-ex-prompt ex-file))
  1112.  
  1113. ;; splits the string FILESPEC into substrings separated by newlines `\012'
  1114. ;; each line assumed to be a file name. find-file's each file thus obtained.
  1115. (defun ex-find-file (filespec)
  1116.   (let (f filebuf tmp-buf status)
  1117.     (if (string-match "[^a-zA-Z0-9_.-/]" filespec)
  1118.     (progn
  1119.       (save-excursion 
  1120.         (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
  1121.         (erase-buffer)
  1122.         (setq status
  1123.           (call-process ex-find-file-shell nil t nil
  1124.                 ex-find-file-shell-options 
  1125.                 "-c"
  1126.                 (format "echo %s | tr ' ' '\\012'" filespec)))
  1127.         (goto-char (point-min))
  1128.         ;; Issue an error, if no match.
  1129.         (if (> status 0)
  1130.         (save-excursion
  1131.           (skip-chars-forward " \t\n\j")
  1132.           (if (looking-at "echo:")
  1133.               (vip-forward-word 1))
  1134.           (error "%S%s"
  1135.              filespec
  1136.              (buffer-substring (point) (vip-line-pos 'end)))
  1137.           ))
  1138.         (reverse-region (point-min) (point-max))
  1139.         (goto-char (point-min))
  1140.         (while (not (eobp))
  1141.           (setq f (buffer-substring (point) (vip-line-pos 'end)))
  1142.           (setq filebuf (find-file f))
  1143.           (set-buffer tmp-buf) ; otherwise it'll be in f.
  1144.           (forward-to-indentation 1))
  1145.         ))
  1146.       (setq filebuf (find-file-noselect (setq f filespec))))
  1147.     (switch-to-buffer filebuf)
  1148.     ))
  1149.  
  1150. ;; Ex global command
  1151. (defun ex-global (variant)
  1152.   (let ((gcommand ex-token))
  1153.     (if (or ex-g-flag ex-g-variant)
  1154.     (error "`%s' within `global' is not allowed" gcommand)
  1155.       (if variant
  1156.       (setq ex-g-flag nil
  1157.         ex-g-variant t)
  1158.     (setq ex-g-flag t
  1159.           ex-g-variant nil)))
  1160.     (vip-get-ex-pat)
  1161.     (if (null ex-token)
  1162.     (error "`%s': Missing regular expression" gcommand)))
  1163.   
  1164.   (if (string= ex-token "")
  1165.       (if (null vip-s-string)
  1166.       (error vip-NoPrevSearch)
  1167.     (setq ex-g-pat vip-s-string))
  1168.     (setq ex-g-pat ex-token
  1169.       vip-s-string ex-token))
  1170.   (if (null ex-addresses)
  1171.       (setq ex-addresses (list (point-max) (point-min)))
  1172.     (vip-default-ex-addresses))
  1173.   (let ((marks nil) (mark-count 0)
  1174.     com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1175.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1176.     (save-excursion
  1177.       (vip-enlarge-region beg end)
  1178.       (exchange-point-and-mark)
  1179.       (let ((cont t) (limit (point-marker)))
  1180.     (exchange-point-and-mark)
  1181.     ;; skip the last line if empty
  1182.     (beginning-of-line)
  1183.     (if (eobp) (vip-backward-char-carefully))
  1184.     (while (and cont (not (bobp)) (>= (point) limit))
  1185.       (beginning-of-line)
  1186.       (set-mark (point))
  1187.       (end-of-line)
  1188.       (let ((found (re-search-backward ex-g-pat (mark t) t)))
  1189.         (if (or (and ex-g-flag found)
  1190.             (and ex-g-variant (not found)))
  1191.         (progn
  1192.           (end-of-line)
  1193.           (setq mark-count (1+ mark-count))
  1194.           (setq marks (cons (point-marker) marks)))))
  1195.       (beginning-of-line)
  1196.       (if (bobp) (setq cont nil)
  1197.         (forward-line -1)
  1198.         (end-of-line)))))
  1199.     (save-window-excursion
  1200.       (set-buffer vip-ex-work-buf)
  1201.       (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
  1202.     (while marks
  1203.       (goto-char (car marks))
  1204.       (vip-ex com-str)
  1205.       (setq mark-count (1- mark-count))
  1206.       (setq marks (cdr marks)))))
  1207.  
  1208. ;; Ex goto command
  1209. (defun ex-goto ()
  1210.   (if (null ex-addresses)
  1211.       (setq ex-addresses (cons (point) nil)))
  1212.   (push-mark (point) t)
  1213.   (goto-char (car ex-addresses))
  1214.   (beginning-of-line))
  1215.  
  1216. ;; Ex line commands.  COM is join, shift-right or shift-left
  1217. (defun ex-line (com)
  1218.   (vip-default-ex-addresses)
  1219.   (vip-get-ex-count)
  1220.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
  1221.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1222.     (save-excursion
  1223.       (vip-enlarge-region beg end)
  1224.       (exchange-point-and-mark)
  1225.       (if ex-count
  1226.       (progn
  1227.         (set-mark (point))
  1228.         (forward-line ex-count)))
  1229.       (if ex-flag
  1230.       ;; show text to be joined and ask for confirmation
  1231.       (progn
  1232.         (with-output-to-temp-buffer " *text*"
  1233.           (princ (buffer-substring (point) (mark t))))
  1234.         (condition-case nil
  1235.         (progn
  1236.           (read-string "[Hit return to continue] ")
  1237.           (ex-line-subr com (point) (mark t)))
  1238.           (quit (ding)))
  1239.         (save-excursion (kill-buffer " *text*")))
  1240.     (ex-line-subr com (point) (mark t)))
  1241.       (setq point (point)))
  1242.     (goto-char (1- point))
  1243.     (beginning-of-line)))
  1244.  
  1245. (defun ex-line-subr (com beg end)
  1246.   (cond ((string= com "join")
  1247.      (goto-char (min beg end))
  1248.      (while (and (not (eobp)) (< (point) (max beg end)))
  1249.        (end-of-line)
  1250.        (if (and (<= (point) (max beg end)) (not (eobp)))
  1251.            (progn
  1252.          (forward-line 1)
  1253.          (delete-region (point) (1- (point)))
  1254.          (if (not ex-variant) (fixup-whitespace))))))
  1255.     ((or (string= com "right") (string= com "left"))
  1256.      (indent-rigidly
  1257.       (min beg end) (max beg end)
  1258.       (if (string= com "right") vip-shift-width (- vip-shift-width)))
  1259.      (goto-char (max beg end))
  1260.      (end-of-line)
  1261.      (vip-forward-char-carefully))))
  1262.  
  1263.  
  1264. ;; Ex mark command
  1265. (defun ex-mark ()
  1266.   (let (char)
  1267.     (if (null ex-addresses)
  1268.     (setq ex-addresses
  1269.           (cons (point) nil)))
  1270.     (save-window-excursion
  1271.       (set-buffer vip-ex-work-buf)
  1272.       (skip-chars-forward " \t")
  1273.       (if (looking-at "[a-z]")
  1274.       (progn
  1275.         (setq char (following-char))
  1276.         (forward-char 1)
  1277.         (skip-chars-forward " \t")
  1278.         (if (not (looking-at "[\n|]"))
  1279.         (error "`%s': %s" ex-token vip-SpuriousText)))
  1280.     (error "`%s' requires a following letter" ex-token)))
  1281.     (save-excursion
  1282.       (goto-char (car ex-addresses))
  1283.       (point-to-register (1+ (- char ?a))))))
  1284.  
  1285.     
  1286.       
  1287. ;; Alternate file is the file next to the first one in the buffer ring
  1288. (defun ex-next (cycle-other-window &optional find-alt-file)
  1289.   (catch 'ex-edit
  1290.     (let (count l)
  1291.       (if (not find-alt-file) 
  1292.       (progn
  1293.         (vip-get-ex-file)
  1294.         (if (or (char-or-string-p ex-offset)
  1295.             (and (not (string= "" ex-file)) 
  1296.                  (not (string-match "^[0-9]+$" ex-file))))
  1297.         (progn
  1298.           (ex-edit t)
  1299.           (throw 'ex-edit nil))
  1300.           (setq count (string-to-int ex-file))
  1301.           (if (= count 0) (setq count 1))
  1302.           (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
  1303.     (setq count 1))
  1304.       (setq l (vip-undisplayed-files))
  1305.       (while (> count 0)
  1306.     (while (and (not (null l)) (null (car l)))
  1307.       (setq l (cdr l)))
  1308.     (setq count (1- count))
  1309.     (if (> count 0)
  1310.         (setq l (cdr l))))
  1311.       (if find-alt-file (car l)
  1312.     (progn
  1313.       (if (car l)
  1314.           (let* ((w (if cycle-other-window
  1315.                 (get-lru-window) (selected-window)))
  1316.              (b (window-buffer w)))
  1317.         (set-window-buffer w (get-file-buffer (car l)))
  1318.         (bury-buffer b)
  1319.         ;; this puts "next <count>" in the ex-command history
  1320.         (ex-fixup-history vip-last-ex-prompt ex-file))
  1321.         (error "Not that many undisplayed files")))))))
  1322.  
  1323.  
  1324. (defun ex-next-related-buffer (direction &optional no-recursion)
  1325.   
  1326.   (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
  1327.   
  1328.   (let ((file-or-buffer-name 
  1329.      (vip-current-ring-item vip-related-files-and-buffers-ring))
  1330.     (old-ring vip-related-files-and-buffers-ring)
  1331.     (old-win (selected-window))
  1332.     skip-rest buf wind)
  1333.     
  1334.     (or (and (ring-p vip-related-files-and-buffers-ring)
  1335.          (> (ring-length vip-related-files-and-buffers-ring) 0))
  1336.     (error "This buffer has no related files or buffers"))
  1337.     
  1338.     (or (stringp file-or-buffer-name)
  1339.     (error
  1340.      "File and buffer names must be strings, %S" file-or-buffer-name))
  1341.     
  1342.     (setq buf (cond ((get-buffer file-or-buffer-name))
  1343.             ((file-exists-p file-or-buffer-name)
  1344.              (find-file-noselect file-or-buffer-name))
  1345.             ))
  1346.     
  1347.     (if (not (vip-buffer-live-p buf))
  1348.     (error "Didn't find buffer %S or file %S"
  1349.            file-or-buffer-name
  1350.            (abbreviate-file-name (expand-file-name file-or-buffer-name))))
  1351.       
  1352.     (if (equal buf (current-buffer))
  1353.     (or no-recursion
  1354.         ;; try again
  1355.         (setq skip-rest t)
  1356.         (ex-next-related-buffer direction 'norecursion)))
  1357.     
  1358.     (if skip-rest
  1359.     ()
  1360.       ;; setup buffer
  1361.       (if (setq wind (vip-get-visible-buffer-window buf))
  1362.       ()
  1363.     (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
  1364.     (set-window-buffer wind buf))
  1365.         
  1366.       (if (vip-window-display-p)
  1367.       (progn
  1368.         (raise-frame (window-frame wind))
  1369.         (if (equal (window-frame wind) (window-frame old-win))
  1370.         (save-window-excursion (select-window wind) (sit-for 1))
  1371.           (select-window wind)))
  1372.     (save-window-excursion (select-window wind) (sit-for 1)))
  1373.     
  1374.       (save-excursion
  1375.     (set-buffer buf)
  1376.     (setq vip-related-files-and-buffers-ring old-ring))
  1377.       
  1378.       (setq vip-local-search-start-marker (point-marker))
  1379.       )))
  1380.   
  1381.     
  1382. ;; Force auto save
  1383. (defun ex-preserve ()
  1384.   (message "Autosaving all buffers that need to be saved...")
  1385.   (do-auto-save t))
  1386.  
  1387. ;; Ex put
  1388. (defun ex-put ()
  1389.   (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
  1390.     (vip-get-ex-buffer)
  1391.     (setq vip-use-register ex-buffer)
  1392.     (goto-char point)
  1393.     (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
  1394.  
  1395. ;; Ex print working directory
  1396. (defun ex-pwd ()
  1397.   (message default-directory))
  1398.  
  1399. ;; Ex quit command
  1400. (defun ex-quit ()
  1401.   ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
  1402.   (save-excursion
  1403.     (set-buffer vip-ex-work-buf)
  1404.     (if (looking-at "!") (forward-char 1)))
  1405.   (if (< vip-expert-level 3)
  1406.       (save-buffers-kill-emacs)
  1407.     (kill-buffer (current-buffer))))
  1408.  
  1409.  
  1410. ;; Ex read command
  1411. (defun ex-read ()
  1412.   (vip-get-ex-file)
  1413.   (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
  1414.     (goto-char point)
  1415.     (vip-add-newline-at-eob-if-necessary)
  1416.     (if (not (or (bobp) (eobp))) (forward-line 1))
  1417.     (if (and (not ex-variant) (string= ex-file ""))
  1418.     (progn
  1419.       (if (null buffer-file-name)
  1420.           (error vip-NoFileSpecified))
  1421.       (setq ex-file buffer-file-name)))
  1422.     (if ex-cmdfile
  1423.     (shell-command ex-file t)
  1424.       (insert-file-contents ex-file)))
  1425.   (ex-fixup-history vip-last-ex-prompt ex-file))
  1426.   
  1427. ;; this function fixes ex-history for some commands like ex-read, ex-edit
  1428. (defun ex-fixup-history (&rest args)  
  1429.   (setq vip-ex-history
  1430.     (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
  1431.   
  1432.  
  1433. ;; Ex recover from emacs \#file\#
  1434. (defun ex-recover ()
  1435.   (vip-get-ex-file)
  1436.   (if (or ex-append ex-offset)
  1437.       (error "`recover': %s" vip-SpuriousText))
  1438.   (if (string= ex-file "")
  1439.       (progn
  1440.     (if (null buffer-file-name)
  1441.         (error "This buffer isn't visiting any file"))
  1442.     (setq ex-file buffer-file-name))
  1443.     (setq ex-file (expand-file-name ex-file)))
  1444.   (if (and (not (string= ex-file (buffer-file-name)))
  1445.        (buffer-modified-p)
  1446.        (not ex-variant))
  1447.       (error "No write since last change \(:rec! overrides\)"))
  1448.   (recover-file ex-file))
  1449.  
  1450. ;; Tell that `rewind' is obsolete and to use `:next count' instead
  1451. (defun ex-rewind ()
  1452.   (message
  1453.    "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
  1454.  
  1455.  
  1456. ;; read variable name for ex-set
  1457. (defun ex-set-read-variable ()
  1458.   (let ((minibuffer-local-completion-map
  1459.      (copy-keymap minibuffer-local-completion-map))
  1460.     (cursor-in-echo-area t)
  1461.     str batch)
  1462.     (define-key
  1463.       minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
  1464.     (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
  1465.     (if (vip-set-unread-command-events
  1466.      (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
  1467.     (progn
  1468.       (setq batch t)
  1469.       (vip-set-unread-command-events ?\C-m)))
  1470.     (message ":set  <Variable> [= <Value>]")
  1471.     (or batch (sit-for 2))
  1472.     
  1473.     (while (string-match "^[ \\t\\n]*$"
  1474.              (setq str
  1475.                    (completing-read ":set " ex-variable-alist)))
  1476.       (message ":set <Variable> ")
  1477.       ;; if there are unread events, don't wait
  1478.       (or (vip-set-unread-command-events "") (sit-for 2))
  1479.       ) ; while
  1480.     str))
  1481.  
  1482.  
  1483. (defun ex-set ()
  1484.   (let ((var (ex-set-read-variable))
  1485.     (val 0)
  1486.     (set-cmd "setq")
  1487.     (ask-if-save t)
  1488.     (auto-cmd-label "; don't touch or else...")
  1489.     (delete-turn-on-auto-fill-pattern
  1490.      "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
  1491.     actual-lisp-cmd lisp-cmd-del-pattern
  1492.     val2 orig-var)
  1493.     (setq orig-var var)
  1494.     (cond ((member var '("ai" "autoindent"))
  1495.        (setq var "vip-auto-indent"
  1496.          val "t"))
  1497.       ((member var '("noai" "noautoindent"))
  1498.        (setq var "vip-auto-indent"
  1499.          val "nil"))
  1500.       ((member var '("ic" "ignorecase"))
  1501.        (setq var "vip-case-fold-search"
  1502.          val "t"))
  1503.       ((member var '("noic" "noignorecase"))
  1504.        (setq var "vip-case-fold-search"
  1505.          val "nil"))
  1506.       ((member var '("ma" "magic"))
  1507.        (setq var "vip-re-search"
  1508.          val "t"))
  1509.       ((member var '("noma" "nomagic"))
  1510.        (setq var "vip-re-search"
  1511.          val "nil"))
  1512.       ((member var '("ro" "readonly"))
  1513.        (setq var "buffer-read-only"
  1514.          val "t"))
  1515.       ((member var '("noro" "noreadonly"))
  1516.        (setq var "buffer-read-only"
  1517.          val "nil"))
  1518.       ((member var '("sm" "showmatch"))
  1519.        (setq var "blink-matching-paren"
  1520.          val "t"))
  1521.       ((member var '("nosm" "noshowmatch"))
  1522.        (setq var "blink-matching-paren"
  1523.          val "nil"))
  1524.       ((member var '("ws" "wrapscan"))
  1525.        (setq var "vip-search-wrap-around-t"
  1526.          val "t"))
  1527.       ((member var '("nows" "nowrapscan"))
  1528.        (setq var "vip-search-wrap-around-t"
  1529.          val "nil")))
  1530.     (if (eq val 0) ; value must be set by the user
  1531.     (let ((cursor-in-echo-area t))
  1532.       (message (format ":set %s = <Value>" var))
  1533.       ;; if there are unread events, don't wait
  1534.       (or (vip-set-unread-command-events "") (sit-for 2))
  1535.       (setq val (read-string (format ":set %s = " var)))
  1536.       (ex-fixup-history "set" orig-var val)
  1537.       
  1538.       ;; check numerical values
  1539.       (if (member var
  1540.               '("sw" "shiftwidth" "ts" "tabstop" "wm" "wrapmargin"))
  1541.           (condition-case nil
  1542.           (or (numberp (setq val2 (car (read-from-string val))))
  1543.               (error "%s: Invalid value, numberp, %S" var val))
  1544.         (error
  1545.          (error "%s: Invalid value, numberp, %S" var val))))
  1546.           
  1547.       (cond
  1548.        ((member var '("sw" "shiftwidth"))
  1549.         (setq var "vip-shift-width"))
  1550.        ((member var '("ts" "tabstop"))
  1551.         ;; make it take effect in curr buff and new bufs
  1552.         (kill-local-variable 'tab-width)
  1553.         (setq var "tab-width"
  1554.           set-cmd "setq-default"))
  1555.        ((member var '("tsl" "tab-stop-local"))
  1556.         (setq var "tab-width"
  1557.           set-cmd "setq"
  1558.           ask-if-save nil))
  1559.        ((member var '("wm" "wrapmargin"))
  1560.         ;; make it take effect in curr buff and new bufs
  1561.         (kill-local-variable 'fill-column) 
  1562.         (setq var "fill-column" 
  1563.           val (format "(- (window-width) %s)" val)
  1564.           set-cmd "setq-default"))
  1565.        ((member var '("sh" "shell"))
  1566.         (setq var "explicit-shell-file-name"
  1567.           val (format "\"%s\"" val)))))
  1568.       (ex-fixup-history "set" orig-var))
  1569.     
  1570.     (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
  1571.                   set-cmd var val auto-cmd-label))
  1572.     (setq lisp-cmd-del-pattern
  1573.       (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
  1574.           set-cmd var auto-cmd-label))
  1575.     
  1576.     (if (and ask-if-save
  1577.          (y-or-n-p (format "Do you want to save this setting in %s "
  1578.                    vip-custom-file-name)))
  1579.     (progn
  1580.       (vip-save-string-in-file 
  1581.        actual-lisp-cmd vip-custom-file-name
  1582.        ;; del pattern
  1583.        lisp-cmd-del-pattern)
  1584.       (if (string= var "fill-column")
  1585.           (if (> val2 0)
  1586.           (vip-save-string-in-file
  1587.            (concat
  1588.             "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
  1589.             auto-cmd-label)
  1590.            vip-custom-file-name
  1591.            delete-turn-on-auto-fill-pattern)
  1592.         (vip-save-string-in-file
  1593.          nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
  1594.         (vip-save-string-in-file
  1595.          nil vip-custom-file-name
  1596.          ;; del pattern
  1597.          lisp-cmd-del-pattern)
  1598.         ))
  1599.       ))
  1600.     
  1601.     (message (format "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
  1602.                         (format "%S" val)
  1603.                           val)))
  1604.     (eval (car (read-from-string actual-lisp-cmd)))
  1605.     (if (string= var "fill-column")
  1606.         (if (> val2 0)
  1607.             (auto-fill-mode 1)
  1608.           (auto-fill-mode -1)))
  1609.         
  1610.     ))
  1611.  
  1612. ;; In inline args, skip regex-forw and (optionally) chars-back.
  1613. ;; Optional 3d arg is a string that should replace ' ' to prevent its
  1614. ;; special meaning
  1615. (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
  1616.   (save-excursion
  1617.     (set-buffer vip-ex-work-buf)
  1618.     (goto-char (point-min))
  1619.     (re-search-forward regex-forw nil t)
  1620.     (let ((beg (point))
  1621.       end)
  1622.       (goto-char (point-max))
  1623.       (if chars-back
  1624.       (skip-chars-backward chars-back)
  1625.     (skip-chars-backward " \t\n\C-m"))
  1626.       (setq end (point))
  1627.       ;; replace SPC with `=' to suppress the special meaning SPC has
  1628.       ;; in Ex commands
  1629.       (goto-char beg)
  1630.       (if replace-str
  1631.       (while (re-search-forward " +" nil t)
  1632.         (replace-match replace-str nil t)
  1633.         (vip-forward-char-carefully)))
  1634.       (goto-char end)
  1635.       (buffer-substring beg end))))
  1636.  
  1637.  
  1638. ;; Ex shell command
  1639. (defun ex-shell ()
  1640.   (shell))
  1641.   
  1642. ;; Viper help. Invokes Info
  1643. (defun ex-help ()
  1644.   (condition-case nil
  1645.       (progn
  1646.     (pop-to-buffer (get-buffer-create "*info*"))
  1647.     (info "viper.info")
  1648.     (message "Type `i' to search for a specific topic"))
  1649.     (error (beep 1)
  1650.        (with-output-to-temp-buffer " *vip-info*"
  1651.          (princ (format "
  1652. The Info file for Viper does not seem to be installed.
  1653.  
  1654. This file is part of the standard distribution of %sEmacs.
  1655. Please contact your system administrator. "
  1656.                 (if vip-xemacs-p "X" "")
  1657.                 ))))))
  1658.  
  1659. ;; Ex source command. Loads the file specified as argument or `~/.vip'
  1660. (defun ex-source ()
  1661.   (vip-get-ex-file)
  1662.   (if (string= ex-file "")
  1663.       (load vip-custom-file-name)
  1664.     (load ex-file)))
  1665.  
  1666. ;; Ex substitute command
  1667. ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
  1668. (defun ex-substitute (&optional repeat r-flag) 
  1669.   (let ((opt-g nil)
  1670.     (opt-c nil)
  1671.     (matched-pos nil)
  1672.     (case-fold-search vip-case-fold-search)
  1673.     delim pat repl)
  1674.     (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
  1675.     (if (null ex-token)
  1676.     (progn
  1677.       (setq pat (if r-flag vip-s-string ex-reg-exp))
  1678.       (or (stringp pat)
  1679.           (error "No previous pattern to use in substitution"))
  1680.       (setq repl ex-repl
  1681.         delim (string-to-char pat)))
  1682.       (setq pat (if (string= ex-token "") vip-s-string ex-token))
  1683.       (setq vip-s-string pat
  1684.         ex-reg-exp pat)
  1685.       (setq delim (vip-get-ex-pat))
  1686.       (if (null ex-token)
  1687.       (setq ex-token ""
  1688.         ex-repl "")
  1689.     (setq repl ex-token
  1690.           ex-repl ex-token)))
  1691.     (while (vip-get-ex-opt-gc delim)
  1692.       (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
  1693.     (vip-get-ex-count)
  1694.     (if ex-count
  1695.     (save-excursion
  1696.       (if ex-addresses (goto-char (car ex-addresses)))
  1697.       (set-mark (point))
  1698.       (forward-line (1- ex-count))
  1699.       (setq ex-addresses (cons (point) (cons (mark t) nil))))
  1700.       (if (null ex-addresses)
  1701.       (setq ex-addresses (cons (point) (cons (point) nil)))
  1702.     (if (null (cdr ex-addresses))
  1703.         (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
  1704.                     ;(setq G opt-g)
  1705.     (let ((beg (car ex-addresses))
  1706.       (end (car (cdr ex-addresses)))
  1707.       eol-mark)
  1708.       (save-excursion
  1709.     (vip-enlarge-region beg end)
  1710.     (let ((limit (save-excursion
  1711.                (goto-char (max (point) (mark t)))
  1712.                (point-marker))))
  1713.       (goto-char (min (point) (mark t)))
  1714.       (while (< (point) limit)
  1715.         (end-of-line)
  1716.         (setq eol-mark (point-marker))
  1717.         (beginning-of-line)
  1718.         (if opt-g
  1719.         (progn
  1720.           (while (and (not (eolp))
  1721.                   (re-search-forward pat eol-mark t))
  1722.             (if (or (not opt-c) (y-or-n-p "Replace? "))
  1723.             (progn
  1724.               (setq matched-pos (point))
  1725.               (if (not (stringp repl))
  1726.                   (error "Can't perform Ex substitution: No previous replacement pattern"))
  1727.               (replace-match repl t t))))
  1728.           (end-of-line)
  1729.           (vip-forward-char-carefully))
  1730.           (if (null pat)
  1731.           (error
  1732.            "Can't repeat Ex substitution: No previous regular expression"))
  1733.           (if (and (re-search-forward pat eol-mark t)
  1734.                (or (not opt-c) (y-or-n-p "Replace? ")))
  1735.           (progn
  1736.             (setq matched-pos (point))
  1737.             (if (not (stringp repl))
  1738.             (error "Can't perform Ex substitution: No previous replacement pattern"))
  1739.             (replace-match repl t t)))
  1740.           (end-of-line)
  1741.           (vip-forward-char-carefully))))))
  1742.     (if matched-pos (goto-char matched-pos))
  1743.     (beginning-of-line)
  1744.     (if opt-c (message "done"))))
  1745.  
  1746. ;; Ex tag command
  1747. (defun ex-tag ()
  1748.   (let (tag)
  1749.     (save-window-excursion
  1750.       (set-buffer vip-ex-work-buf)
  1751.       (skip-chars-forward " \t")
  1752.       (set-mark (point))
  1753.       (skip-chars-forward "^ |\t\n")
  1754.       (setq tag (buffer-substring (mark t) (point))))
  1755.     (if (not (string= tag "")) (setq ex-tag tag))
  1756.     (vip-change-state-to-emacs)
  1757.     (condition-case conds
  1758.     (progn
  1759.       (if (string= tag "")
  1760.           (find-tag ex-tag t)
  1761.         (find-tag-other-window ex-tag))
  1762.       (vip-change-state-to-vi))
  1763.       (error
  1764.        (vip-change-state-to-vi)
  1765.        (vip-message-conditions conds)))))
  1766.  
  1767. ;; Ex write command
  1768. (defun ex-write (q-flag)
  1769.   (vip-default-ex-addresses t)
  1770.   (vip-get-ex-file)
  1771.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) 
  1772.     temp-buf writing-same-file region
  1773.     file-exists writing-whole-file)
  1774.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1775.     (if ex-cmdfile
  1776.     (progn
  1777.       (vip-enlarge-region beg end)
  1778.       (shell-command-on-region (point) (mark t) ex-file))
  1779.       (if (and (string= ex-file "") (not (buffer-file-name)))
  1780.       (setq ex-file
  1781.         (read-file-name
  1782.          (format "Buffer %s isn't visiting any file. File to save in: "
  1783.              (buffer-name)))))
  1784.       
  1785.       (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
  1786.         ex-file (if (string= ex-file "")
  1787.             (buffer-file-name)
  1788.               (expand-file-name ex-file))
  1789.         file-exists (file-exists-p ex-file)
  1790.         writing-same-file (string= ex-file (buffer-file-name)))
  1791.       (if (and writing-whole-file writing-same-file)
  1792.       (if (not (buffer-modified-p))
  1793.           (message "(No changes need to be saved)")
  1794.         (save-buffer)
  1795.         (ex-write-info file-exists ex-file beg end))
  1796.     ;; writing some other file or portion of the currents
  1797.     ;; file---create temp buffer for it
  1798.     ;; disable undo in that buffer, for efficiency
  1799.     (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
  1800.     (unwind-protect 
  1801.         (save-excursion
  1802.           (if (and file-exists
  1803.                (not writing-same-file)
  1804.                (not (yes-or-no-p
  1805.                  (format "File %s exists. Overwrite? " ex-file))))
  1806.           (error "Quit")
  1807.         (vip-enlarge-region beg end)
  1808.         (setq region (buffer-substring (point) (mark t)))
  1809.         (set-buffer temp-buf)
  1810.         (set-visited-file-name ex-file)
  1811.         (erase-buffer)
  1812.         (if (and file-exists ex-append)
  1813.             (insert-file-contents ex-file))
  1814.         (goto-char (point-max))
  1815.         (insert region)
  1816.         (save-buffer)
  1817.         (ex-write-info file-exists ex-file (point-min) (point-max))
  1818.         )
  1819.           (set-buffer temp-buf)
  1820.           (set-buffer-modified-p nil)
  1821.           (kill-buffer temp-buf)
  1822.           )
  1823.       ))
  1824.       ;; this prevents the loss of data if writing part of the buffer
  1825.       (if (and (buffer-file-name) writing-same-file)
  1826.       (set-visited-file-modtime))
  1827.       (or writing-whole-file 
  1828.       (not writing-same-file)
  1829.       (set-buffer-modified-p t))
  1830.       (if q-flag
  1831.       (if (< vip-expert-level 2)
  1832.           (save-buffers-kill-emacs)
  1833.         (kill-buffer (current-buffer))))
  1834.       )))
  1835.       
  1836.  
  1837. (defun ex-write-info (exists file-name beg end)
  1838.   (message "`%s'%s %d lines, %d characters"
  1839.        (abbreviate-file-name file-name)
  1840.        (if exists "" " [New file]")
  1841.        (count-lines beg (min (1+ end) (point-max)))
  1842.        (- end beg)))
  1843.  
  1844. ;; Ex yank command
  1845. (defun ex-yank ()
  1846.   (vip-default-ex-addresses)
  1847.   (vip-get-ex-buffer)
  1848.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1849.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1850.     (save-excursion
  1851.       (vip-enlarge-region beg end)
  1852.       (exchange-point-and-mark)
  1853.       (if (or ex-g-flag ex-g-variant)
  1854.       (error "Can't execute `yank' within `global'"))
  1855.       (if ex-count
  1856.       (progn
  1857.         (set-mark (point))
  1858.         (forward-line (1- ex-count)))
  1859.     (set-mark end))
  1860.       (vip-enlarge-region (point) (mark t))
  1861.       (if ex-flag (error "`yank': %s" vip-SpuriousText))
  1862.       (if ex-buffer
  1863.       (cond ((vip-valid-register ex-buffer '(Letter))
  1864.          (vip-append-to-register
  1865.           (downcase ex-buffer) (point) (mark t)))
  1866.         ((vip-valid-register ex-buffer)
  1867.          (copy-to-register ex-buffer (point) (mark t) nil))
  1868.         (t (error vip-InvalidRegister ex-buffer))))
  1869.       (copy-region-as-kill (point) (mark t)))))
  1870.  
  1871. ;; Execute shell command
  1872. (defun ex-command ()
  1873.   (let (command)
  1874.     (save-window-excursion
  1875.       (set-buffer vip-ex-work-buf)
  1876.       (skip-chars-forward " \t")
  1877.       (setq command (buffer-substring (point) (point-max)))
  1878.       (end-of-line))
  1879.     (setq command (ex-expand-filsyms command (current-buffer)))
  1880.     (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
  1881.     (if vip-ex-last-shell-com
  1882.         (setq command (concat vip-ex-last-shell-com (substring command 1)))
  1883.       (error "No previous shell command")))
  1884.     (setq vip-ex-last-shell-com command)
  1885.     (if (null ex-addresses)
  1886.     (shell-command command)
  1887.       (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1888.     (if (null beg) (setq beg end))
  1889.     (save-excursion
  1890.       (goto-char beg)
  1891.       (set-mark end)
  1892.       (vip-enlarge-region (point) (mark t))
  1893.       (shell-command-on-region (point) (mark t) command t))
  1894.     (goto-char beg)))))
  1895.  
  1896. ;; Print line number
  1897. (defun ex-line-no ()
  1898.   (message "%d"
  1899.        (1+ (count-lines
  1900.         (point-min)
  1901.         (if (null ex-addresses) (point-max) (car ex-addresses))))))
  1902.  
  1903. ;; Give information on the file visited by the current buffer
  1904. (defun vip-info-on-file ()
  1905.   (interactive)
  1906.   (let (file info)
  1907.     (setq file (if (buffer-file-name)
  1908.            (concat (abbreviate-file-name (buffer-file-name)) ":")
  1909.          (concat (buffer-name) " [Not visiting any file]:"))
  1910.       info (format "line=%d/%d pos=%d/%d col=%d %s"
  1911.                (count-lines (point-min) (vip-line-pos 'end))
  1912.                (count-lines (point-min) (point-max))
  1913.                (point) (1- (point-max))
  1914.                (1+ (current-column))
  1915.                (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
  1916.     (if (< (+ 1 (length info) (length file))
  1917.        (window-width (minibuffer-window)))
  1918.     (message (concat file " " info))
  1919.       (save-window-excursion
  1920.     (with-output-to-temp-buffer " *vip-info*"
  1921.       (princ (concat "\n"
  1922.              file "\n\n\t" info
  1923.              "\n\n\nPress any key to continue...\n\n")))
  1924.     (vip-read-event)))
  1925.     ))
  1926.  
  1927.  
  1928. (provide 'viper-ex)
  1929.  
  1930. ;;;  viper-ex.el ends here
  1931.